package gxdlxy.zx2202.chat5;

public class Example1 {
    public static void main(String[] args) {
        String str = "The setting sun is reflected from the windows of the alms-house as brightly as from the rich " +
                "man's abode; the snow melts before its door as early in the spring. I do not see but a quiet mind " +
                "may live as contentedly there, and have as cheering thoughts, as in a palace. The town's poor seem " +
                "to me often to live the most independent lives of any. May be they are simply great enough to " +
                "receive without misgiving. Most think that they are above being supported by the town; but it often " +
                "happens that they are not above supporting themselves by dishonest means. Which should be more " +
                "disreputable. Cultivate poverty like a garden herb, like sage.";
        // TODO 每个单词出现的次数（不区分大小写）
        char[] cs = str.toLowerCase().toCharArray();
        // 过滤非字母字符和空格字符
        char[] cs_p = new char[cs.length];
        int count = 0;
        for (int i = 0; i < cs.length; i++)
            if (cs[i] >= 'a' && cs[i] <= 'z' || cs[i] == ' ')
                cs_p[count++] = cs[i];
        cs_p[count] = ' ';
        str = String.valueOf(cs_p);
        System.out.println(str);
        String[] words = str.split(" ");

        int i, j = 0;
        count = 0;
        String[] wordDiff = new String[words.length];
        for (i = 0; i < words.length - 1; i++) {
            for (j = 0; j < count; j++)
                if (words[i].equals(wordDiff[j]))
                    break;
            if (j == count)
                wordDiff[count++] = words[i];
        }
        int[] cnts = new int[wordDiff.length];
        for (i = 0; i < words.length - 1; i++)
            for (j = 0; j < wordDiff.length; j++)
                if (words[i].equals(wordDiff[j])) {
                    cnts[j]++;
                    break;
                }
        for(i=0; i< count; i++)
            System.out.println(wordDiff[i] + ":" + cnts[i]);
    }
}
